package org.hot100_2;

import java.util.ArrayList;
import java.util.List;

public class leetcode131 {

    List<List<String>> res = new ArrayList<>();
    List<String> item = new ArrayList<>();
    public List<List<String>> partition(String s) {
        backtracking(s,0);
        return res;
    }

    private void backtracking(String s,int index){
        if (index == s.length()){
            res.add(new ArrayList<>(item));
            return;
        }

        for (int i = index; i < s.length(); i++) {
            if (isPartition(s,index,i)){
                String partition = s.substring(index, i+1);
                item.add(partition);
                backtracking(s,i+1);
                item.remove(item.size()-1);
            }
        }
    }

    private boolean isPartition(String s,int left,int right){

        while (left<right){
            if (s.charAt(left)!=s.charAt(right)){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}
